package com.yf.controller.api.im;


import com.yf.annotation.LoginUser;
import com.yf.entity.GroupUserEntity;
import com.yf.entity.MsgGroupEntity;
import com.yf.entity.UserEntity;
import com.yf.response.R;
import com.yf.service.im.MsgGroupService;
import com.yf.vo.MsgGroupUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author zhoujie
 * 群聊
 * ApiMsgGroupController
 */
@RestController
@RequestMapping("/api/msg/group")
public class ApiMsgGroupController {

    @Autowired
    private MsgGroupService msgGroupService;


    /************重写下面接口*****************/

    /**
     * 创建或者修改当前用户的群
     *
     * @param userEntity
     * @param msgGroupEntity
     * @return
     */
    @PostMapping("/merge")
    public R create(@LoginUser UserEntity userEntity, MsgGroupEntity msgGroupEntity) {
        return R.ok(msgGroupService.merge(userEntity.getId(), msgGroupEntity));
    }

    /**
     * 群的所有信息
     *
     * @param userEntity
     * @param gid
     * @return
     */
    @PostMapping("/info")
    public R info(@LoginUser UserEntity userEntity, Long gid) {
        return R.ok(msgGroupService.findGroupUsers(userEntity.getId(), gid));
    }

    /**
     * 查找自己创建的群 跟 自己加入的群
     *
     * @param userEntity
     * @return
     */
    @GetMapping("/list")
    public R list(@LoginUser UserEntity userEntity) {
        List<MsgGroupUserVo> list = msgGroupService.findMsgGroupByUserId(userEntity.getId());
        return R.ok(list);
    }

    /**
     * 根据群Id查找群
     *
     * @param gid
     * @return
     */
    @GetMapping("/find")
    public R list(Long gid) {
        return R.ok(msgGroupService.get(gid));
    }


    /**
     * 退出群聊,群主退出删除群聊，成员只退群
     *
     * @param userEntity
     * @param gid
     * @return
     */
    @PostMapping("/out")
    public R out(@LoginUser UserEntity userEntity, Long gid) {

        return R.ok(msgGroupService.outGroup(userEntity.getId(), gid));
    }


    /***************IM操作接口***************/


    /**
     * 加入群聊
     *
     * @param userEntity
     * @param gid
     * @return
     */
    @PostMapping("/join")
    public R join(@LoginUser UserEntity userEntity, @RequestParam(required = false) Long groupId, @RequestParam(required = false) List<Long> userIds) {
        MsgGroupEntity msgGroupEntity = msgGroupService.joinGroup(userEntity.getId(), groupId, userIds);
        return R.ok("加入群聊成功!", msgGroupEntity);
    }


}
